<template>
  <div>
    <!-- 头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 头部添加搜索区域 -->
      <el-row :gutter="20">
        <!-- 搜索 -->
        <el-col :span="8">
            <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query" clearable @clear="getGoodsByName">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsByName"></el-button>
            </el-input>
        </el-col>
        <!-- 添加按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格区域 -->
      <el-table :data="goodsList" stripe border>
        <el-table-column label="#" width="50">
          <template
            slot-scope="scope"
          >{{scope.$index+(queryInfo.pagenum - 1) * queryInfo.pagesize + 1}}</template>
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column label="创建时间" width="150">
          <template slot-scope="scope">
            {{scope.row.add_time | fmtYMDHMS}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="showEditDialog(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="delGoods(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="queryInfo.pagenum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      goodsList: []
    };
  },
  methods: {
    goAddPage(){
      this.$router.push('/goods/add')
    },
    // 根据商品名称模糊查询
    getGoodsByName(){
      this.getGoodsList()
    },
    // 每页显示条数改变时
    handleSizeChange(val){
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 当点击页数时
    handleCurrentChange(val){
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 根据分页获取对应的商品列表
    async getGoodsList(){
      const {data: res} = await this.$http.get('goods',{params: this.queryInfo})
      if(res.meta.status!==200)this.$message.error('获取商品列表失败')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 显示修改商品对话框
    showEditDialog(Id){

    },
    // 删除商品
    async delGoods(Id){
      const resCf = await this.$msbox.confirm("确认删除该商品吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err=>err)
      if(resCf==='cancel') return this.$message.info('已取消删除')
      const {data: res} = await this.$http.delete(`goods/${Id}`)
      if(res.meta.status!==200) return this.$message.error('删除失败')
      this.$message.success('删除成功！')
      this.getGoodsList()
    },
  },
  created(){
    this.getGoodsList()
  },
};
</script>

<style lang='less' scoped>
</style>
